/*
 * MainFrameMenu.java
 *
 *  created: 7.10.2011
 *  charset: UTF-8
 *  license: MIT (X11) (See LICENSE file for full license)
 */
package cz.mp.k3bg.gui;

import cz.mp.k3bg.log.LoggerManager;
import java.util.logging.Logger;
import cz.mp.k3bg.Constants;
import cz.mp.k3bg.Images;
import cz.mp.k3bg.util.DesktopUtils;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import static cz.mp.k3bg.TextSource.*;


/**
 * Hlavní menu programu.
 * 
 * @author Martin Pokorný
 * @version 0.1
 * @see MainFrame
 */
public class MainFrameMenu extends JMenuBar {

    private static final boolean DEBUG = false;
    private static final Logger logger =
            LoggerManager.getLogger(MainFrameMenu.class, DEBUG);
    
    private JMenu menuFile = new JMenu(getLocText("gui.mainmenu.file"));
    private JMenuItem menuFileNewItem = new JMenuItem(getLocText("gui.mainmenu.file.new"));
    private JMenuItem menuFileExitItem = new JMenuItem(getLocText("gui.mainmenu.file.exit"));
    
    private JMenu menuHelp = new JMenu(getLocText("gui.mainmenu.help"));
//    private JMenu menuHelpInternet = new JMenu(getLocText("gui.mainmenu.help.internet"));
    private JMenuItem menuHelpPublishingGuidelinesItem =
            new JMenuItem("Amazon Kindle Publishing Guidelines");
    private JMenuItem menuHelpAboutItem = new JMenuItem(getLocText("gui.mainmenu.help.about"));
       
    // -----
    
    /** */
    public MainFrameMenu() {
        super();
        initMenu();
        initEventHandlers();
    }

    /**
     * 
     */
    private void initMenu() {
        menuFileNewItem.setMnemonic(KeyEvent.VK_N);
        menuFileNewItem.setAccelerator(KeyStroke.getKeyStroke
                  (KeyEvent.VK_N, ActionEvent.CTRL_MASK));
        menuFileNewItem.setIcon(Images.getImage(Images.NEW));

        menuFile.add(menuFileNewItem);
        menuFile.addSeparator();
        menuFile.add(menuFileExitItem);
        this.add(menuFile);
        
        menuHelp.addSeparator();
//        menuHelpInternet.setIcon(Images.getImage(Images.GLOBE));
//        menuHelp.add(menuHelpInternet);
//        menuHelpInternet.add(menuHelpPublishingGuidelinesItem);
        menuHelp.add(menuHelpPublishingGuidelinesItem);
        menuHelp.addSeparator();
        
//        menuHelpAboutItem.setIcon(Images.getImage(Images.INFO));
        menuHelp.add(menuHelpAboutItem);
        
        this.add(menuHelp); 
    }
    
    /**
     * 
     */
    private void initEventHandlers() {
        menuFileNewItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {                
                newBook();
            }
        });
        
        menuHelpPublishingGuidelinesItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                openKindlePublishingGuidelines();
            }
        });

        menuHelpAboutItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                AboutDialog.showDialog();
            }
        });
        
        menuFileExitItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                MainFrame.getInstance().exit(0);
            }
        });
    }
        
    /**
     * 
     */
    private void newBook() {
        logger.fine("");
        int result = JOptionPane.showConfirmDialog(
                MainFrame.getInstance(), 
                getLocText("gui.mainmenu.file.new.dialog_question"),
                getLocText("gui.mainmenu.file.new"),
                JOptionPane.YES_NO_OPTION);
        if (result == JOptionPane.YES_OPTION) {
            logger.info(" new! ");
            MainFrame.getInstance().clearAllPanels();
        }        
    }
    
    /**
     * 
     */
    private void openKindlePublishingGuidelines() {
        logger.fine("");
        try {
            DesktopUtils.openBrowser(
                    Constants.KINDLE_PUBLISHING_GUIDELINES_URL);
        } catch (Exception ex) {
            logger.warning(ex.toString());
            if (DEBUG) {  ex.printStackTrace();  }
            Dialogs.showErrorDialog(ex);
        }        
    }
    
}   // MainFrameMenu.java

